import {
  Column,
  Entity,
  JoinTable,
  ManyToMany,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { Car } from '../../car/entities/car.entity';
import { Service } from '../../service/entities/service.entity';
import { Order } from '../../order/entities/order.entity';
import { UserShopId } from '../../user_shop-id/entities/user_shop-id.entity';
import { v4 as uuidv4 } from 'uuid';
import { Cart } from '../../cart/entities/cart.entity';

@Entity('user')
export class User {
  @PrimaryGeneratedColumn({ type: 'int', comment: 'id' })
  id: number;

  @Column({
    type: 'varchar',
    comment: '用户主 id',
    nullable: false,
    default: () => `'${uuidv4().replace(/-/g, '')}'`,
  })
  primary_id: string;

  @Column({
    type: 'boolean',
    comment: '是否被删除',
    default: false,
  })
  isDelete: boolean;

  @Column({ type: 'varchar', comment: '姓名', nullable: false })
  name: string;

  @Column({ type: 'varchar', comment: '昵称', nullable: true })
  nickname: string;

  @Column({
    type: 'varchar',
    comment: '用户登录的密码',
    select: false,
    nullable: false,
  })
  password: string;

  @Column({
    type: 'varchar',
    comment: '头像',
    nullable: true,
    default: '/vehicle/BiazfanxmamNRoxxVxka.png',
  })
  cover: string;

  @Column({ type: 'varchar', comment: '性别', nullable: true })
  sex: string;

  @Column({ type: 'varchar', comment: '类型', nullable: true, default: 'user' })
  type: string;

  @Column({ type: 'varchar', comment: '省份', nullable: true })
  province: string;

  @Column({ type: 'varchar', comment: '城市', nullable: false })
  city: string;

  @Column({ type: 'int', comment: '年龄', nullable: true })
  age: number;

  @Column({ type: 'varchar', comment: '管理员的手机号码', nullable: false })
  phone: string;

  @Column({
    type: 'varchar',
    length: 30,
    comment: '用户角色，有3中角色，vip 会员，shop 商家，normal普通用户',
    default: 'normal',
    nullable: false,
  })
  role: string;

  @Column({
    type: 'varchar',
    length: '255',
    comment: '用户描述',
    nullable: true,
  })
  description: string;

  @OneToMany(() => Car, (car) => car.user)
  car: Car[];

  @OneToMany(() => Order, (order) => order.user)
  order: Order[];

  @OneToMany(() => Cart, (cart) => cart.user)
  cart: Cart[];

  // 商家发布过的服务
  @OneToMany(() => Service, (ser) => ser.shop)
  publish_service_id: Service[];

  // 这个用户使用过的服务
  @ManyToMany(() => Service, (ser) => ser.user)
  @JoinTable()
  service: Service[];

  // 商家拥有的店铺
  @OneToMany(() => UserShopId, (userShopId) => userShopId.user)
  shopId: UserShopId[];

  @Column({
    type: 'int',
    comment: '账户中的金额',
    default: 10,
    nullable: false,
  })
  account: number;

  @Column({
    name: 'create_date',
    type: 'timestamp',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  create_date: Date;

  @Column({
    name: 'update_date',
    type: 'timestamp',
    comment: '修改时间',
    nullable: true,
  })
  updated_date: Date;
}
